<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-04-24
 * Time: 10:49:53
 * Info: 管理员角色组
 */

namespace app\admin\controller\system;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\admin\controller\CommonController;
use support\Request;
use support\Cache;
use app\common\model\AuthGroup as AuthGroupModel;
use app\common\model\AuthRule as AuthRuleModel;

/**
 * @ControllerAnnotation(title="角色组管理")
 * Class Node
 * @package app\admin\controller\system
 */
class AuthGroupController extends CommonController
{

    /**
     * @NodeAnotation(title="角色组列表")
     */
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $limit = (int)$request->input('limit', 10);
            $page  = (int)$request->input('page', 1);
            $first = (int)($page - 1) * $limit;
            $key   = $request->input('key');
            $where = function ($query) use ($key) {
                if ( ! empty($key['name'])) {
                    $query->whereLike('name', '%'.$key['name'].'%');
                }
                if ( ! empty($key['status'])) {
                    $query->where('status', $key['status']);
                }
            };
            $list  = AuthGroupModel::where($where)->limit($first, $limit)->select()->toArray();
            $total = AuthGroupModel::where($where)->count();

            return $this->success("ok", $list, $total);
        }

        return view('auth_group/index');
    }

    /**
     * @NodeAnotation(title="添加角色组")
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            $this->validate->rule([
                'name|登录名' => 'require',
            ]);
            $this->validate->message([
                'name.require' => '角色组名不能为空',
            ]);
            if ( ! $this->validate->check($param)) {
                return error_json($this->validate->getError());
            }
            $res = AuthGroupModel::manageAdd($param);
            if ($res['code'] == 200) {
                Cache::clear();

                return $this->success($res['msg']);
            } else {
                return error_json($res['msg']);
            }
        }

        return view('auth_group/add');
    }

    /**
     * @NodeAnotation(title="获取角色组信息")
     */
    public function info(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return error_json('权限菜单id不能为空');
        }
        $res = AuthGroupModel::getInfo($id);
        if ($res['code'] == 200) {
            Cache::clear();

            return $this->success($res['msg'], $res['data']);
        } else {
            return error_json($res['msg']);
        }
    }

    /**
     * @NodeAnotation(title="编辑角色组")
     */
    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            $this->validate->rule([
                'name|角色名' => 'require',
            ]);
            $this->validate->message([
                'name.require' => '角色组名不能为空',
            ]);
            $res = AuthGroupModel::manageEdit($param);
            if ($res['code'] == 200) {
                Cache::clear();

                return $this->success($res['msg']);
            } else {
                return error_json($res['msg']);
            }
        }
        $id = $request->get('id');
        if (empty($id)) {
            return error_json('权限菜单id不能为空');
        }
        if ($id == 1) {
            return error_json('超管规则禁止更改');
        }
        $data = AuthGroupModel::getInfo($id);

        return view('auth_group/edit', ['data' => $data['data']]);
    }

    /**
     * @NodeAnotation(title="删除角色组")
     */
    public function delete(Request $request)
    {
        if ($request->isAjax()) {
            $id = $request->get('id');
            if (empty($id)) {
                return error_json('角色组id不能为空');
            }
            if ($id == 1) {
                return error_json('id=1的角色组禁止删除');
            }
            $res = AuthGroupModel::manageDelete($id);
            if ($res['code'] == 200) {
                Cache::clear();

                return $this->success($res['msg']);
            } else {
                return error_json($res['msg']);
            }
        }
    }

    /**
     * @NodeAnotation(title="授权节点输出")
     */
    public function authorize(Request $request)
    {
        if ($request->isAjax()) {
            $id  = $request->get('id');
            $row = AuthGroupModel::where('id', $id)->find();
            if (empty($row)) {
                return error_json('数据不存在');
            }
            $list = AuthRuleModel::getAuthorizeNodeListByAdminId($id);

            return $this->success("ok", $list);
        }
    }

}
